package com.timmy.chi.ui.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.timmy.chi.R;
import com.timmy.chi.adapter.AdminAdapter;
import com.timmy.chi.model.beans.AddFood;
import com.timmy.chi.model.beans.Food;
import com.timmy.chi.utils.network.MyHttp;
import com.timmy.chi.utils.network.ResultCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 管理员页面
 * 所有菜品页面
 */
public class AdminActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {

    @BindView(R.id.admin_listview)
    ListView mListview;
    @BindView(R.id.srl_order)
    SwipeRefreshLayout mSwipe;


    private AdminAdapter mAdminAdapter;
    private Gson mGson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        ButterKnife.bind(this);
        mGson = new Gson();
        initView();
        initData();
    }

    private void initView() {
        mSwipe.setOnRefreshListener(mOnRefreshListener);
        mSwipe.setColorSchemeColors(Color.RED, Color.GREEN, Color.YELLOW);
        mAdminAdapter = new AdminAdapter(this);
        mListview.setAdapter(mAdminAdapter);

        mListview.setOnItemLongClickListener(this);

    }

    /**
     * 加载数据
     */
    private void initData() {
        // 查询所有菜品
        MyHttp.queryAllFood(new ResultCallback() {
            @Override
            public void onSuccess(String s) {
                mSwipe.setRefreshing(false);

                Food food = mGson.fromJson(s, Food.class);
                List<Food.EntryBean> entry = food.getEntry();
                mAdminAdapter.setData(entry);
            }

            @Override
            public void onError() {
                mSwipe.setRefreshing(false);
                Toast.makeText(AdminActivity.this, "请检查当前网络", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initData();
            mSwipe.setRefreshing(false);
        }
    };

    @OnClick({R.id.iv_back, R.id.iv_add})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.iv_back:
                intent = new Intent(this, LogInActivity.class);
                break;
            case R.id.iv_add:
                // 添加商品
                intent = new Intent(this, AddFoodActivity.class);
                break;
        }

        startActivity(intent);
        finish();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

        Food.EntryBean item = mAdminAdapter.getItem(position);
        final int id1 = item.getId();


        Dialog dialog = new AlertDialog.Builder(this).setTitle(null).setMessage("确定要下架该菜品吗？")
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        MyHttp.closeFood(id1, new ResultCallback() {
                            @Override
                            public void onSuccess(String s) {
                                AddFood addFood = mGson.fromJson(s, AddFood.class);
                                if (addFood.getResponseState() == 0) {
                                    Toast.makeText(AdminActivity.this, "下架成功", Toast.LENGTH_SHORT).show();
                                    mAdminAdapter.removeData(position);
                                } else {
                                    Toast.makeText(AdminActivity.this, "下架失败", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError() {
                                Toast.makeText(AdminActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });


                    }
                }).show();
        dialog.setCanceledOnTouchOutside(false);

        return false;
    }
}
